//
//  JFALImagePicker.m
//  SLApp
//
//  Created by QNRZMac on 15/9/15.
//  Copyright (c) 2015年 Sunline. All rights reserved.
//

#import "JFALImagePicker.h"
#import <Photos/Photos.h>


@interface JFALImagePicker ()


@end

@implementation JFALImagePicker


- (instancetype)init {
    self = [super init];
    if (self) {
    }
    return self;
}

+ (instancetype)shareInstance {
    static JFALImagePicker * picker;
    static dispatch_once_t once;
    dispatch_once(&once, ^{
        picker = [[self alloc] init];
    });
    return picker;
}

+ (void)pickImageWithUrl:(NSURL *)url resultBlock:(void (^)(UIImage *))resultBlock{
//    ALAssetsLibrary * assetsLibrary = [JFALImagePicker shareInstance].assetsLibrary;
//    [assetsLibrary assetForURL:url resultBlock:^(ALAsset *asset) {
//        ALAsset *result = asset;
//        CGImageRef ref = [[result  defaultRepresentation] fullResolutionImage];
//        UIImage *img = [[UIImage alloc] initWithCGImage:ref scale:[result  defaultRepresentation].scale orientation:(UIImageOrientation)[result  defaultRepresentation].orientation];
//        !resultBlock ?: resultBlock(img);
//    } failureBlock:^(NSError *error) {
//        !resultBlock ?: resultBlock(nil);
//    }];
    
    __block UIImage * image = nil;
    PHImageManager *imageManager = [PHImageManager defaultManager];
    PHFetchResult *result = [PHAsset fetchAssetsWithALAssetURLs:@[url] options:nil];
    PHAsset *asset = [result firstObject];
    PHImageRequestOptions *options = [[PHImageRequestOptions alloc]init];
    options.synchronous = YES;
    options.version = PHImageRequestOptionsVersionCurrent;
    [imageManager requestImageDataForAsset:asset options:nil resultHandler:^(NSData * _Nullable imageData, NSString * _Nullable dataUTI, UIImageOrientation orientation, NSDictionary * _Nullable info) {
        if (imageData) {
            image = [UIImage imageWithData: imageData];
            resultBlock(image);
        }
    }];
}


+ (UIImage *)pickImageWithUrl:(NSURL*)url {
//    ALAssetsLibrary * assetsLibrary = [SLALImagePicker shareInstance].assetsLibrary;
//    __block BOOL bBusy = YES;
//    __block UIImage * image = nil;
//    [assetsLibrary assetForURL:url resultBlock:^(ALAsset *asset) {
//        ALAsset *result = asset;
//        CGImageRef ref = [[result  defaultRepresentation] fullResolutionImage];
//        image = [[UIImage alloc] initWithCGImage:ref scale:[result  defaultRepresentation].scale orientation:(UIImageOrientation)[result  defaultRepresentation].orientation];
//        bBusy = NO;
//    } failureBlock:^(NSError *error) {
//        bBusy = NO;
//    }];

//    return image;
    
    __block BOOL bBusy = YES;
    __block UIImage * image = nil;
    PHImageManager *imageManager = [PHImageManager defaultManager];
    PHFetchResult *result = [PHAsset fetchAssetsWithALAssetURLs:@[url] options:nil];
    PHAsset *asset = [result firstObject];
    PHImageRequestOptions *options = [[PHImageRequestOptions alloc]init];
    options.synchronous = YES;
    options.version = PHImageRequestOptionsVersionCurrent;
    [imageManager requestImageDataForAsset:asset options:nil resultHandler:^(NSData * _Nullable imageData, NSString * _Nullable dataUTI, UIImageOrientation orientation, NSDictionary * _Nullable info) {
        if (imageData) {
            image = [UIImage imageWithData: imageData];
             bBusy = NO;
        }
        else{
             bBusy = NO;
        }
    }];
    while (bBusy) {
        [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
    }
    return image;

}


@end
